package simulatorUS;
import javax.swing.text.*;

public class JTextFieldLimit extends PlainDocument {
	  /**
	 * 
	 */
	private static final long serialVersionUID = 122454L;
	private int limit;
	  // optional uppercase conversion
	private boolean toUppercase = false;
	private boolean digitsOnly = false;
	  
	  JTextFieldLimit(int limit) {
	   super();
	   this.limit = limit;
	   }
	  
	  JTextFieldLimit(int limit, boolean upper, boolean digitsOnly) {
	   super();
	   this.limit = limit;
	   this.digitsOnly=digitsOnly;
	   toUppercase = upper;
	   }
	 public void insertFiguresOnly(){
		 digitsOnly=true; 
	 }
	  public void insertString
	    (int offset, String  str, AttributeSet attr)
	      throws BadLocationException {
	   if (str == null) return;
       if(digitsOnly){
    	  if(!Character.isDigit(str.charAt(0)))
    		   str="";
       }
	   if ((getLength() + str.length()) <= limit) {
	     if (toUppercase) str = str.toUpperCase();
	     super.insertString(offset, str, attr);
	     }
	   }
	}